<template>
  <el-dialog
    id="createApp"
    title="创建应用"
    :visible.sync="dialogVisible"
    width="900px">
    <el-form :model="appForm" :rules="appRules" ref="appForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="应用名称" prop="appName">
        <el-input v-model="appForm.appName"></el-input>
      </el-form-item>
      <el-form-item class="dialogBtn">
        <el-button @click="reset">重置</el-button>
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
  export default {
    name: 'create-app',
    data () {
      return {
        dialogVisible: false,
        options: {},
        appForm: {
          appName: ''
        },
        appRules: {
          appName: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ]
        }
      }
    },
    watch: {
      dialogVisible (val) {
        if (val === false) {
          this.$refs.appForm.resetFields()
        }
      }
    },
    methods: {
      reset () {
        this.$refs.appForm.resetFields()
      },
      show (options) {
        this.options = options
        this.isAdd = this.options.isAdd
        this.dialogVisible = true
      },
      submitForm () {
        this.$refs.appForm.validate((valid) => {
          if (valid) {
            this.options.onConfirm(this.appForm)
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style lang="stylus">
  #createApp
    .el-form-item__content
      text-align right
</style>
